package com.javabasic.algorithm.leetcode;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/11/9 11:36 下午
 * @see [168. Excel Sheet Column Title](https://leetcode-cn.com/problems/excel-sheet-column-title/)
 */
public class ExcelSheetColumnTitle {

    /**
     * 思维逻辑题：类似求一个26进制的数
     * @param columnNumber
     * @return
     */
    public String convertToTitle(int columnNumber) {
        StringBuffer result = new StringBuffer();
        char tag;
        int size = 26, index;
        while (columnNumber > 0) {
            index = (columnNumber % size) == 0 ? size : (columnNumber % size);
            tag = (char) (index - 1 + 'A');
            result.append(tag);
            if (index == size) {
                // 注意如果低位是26，高位要减1
                columnNumber /= size;
                columnNumber--;
            } else {
                columnNumber /= size;
            }
        }
        result = result.reverse();
        return result.toString();
    }
}
